package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.UserReportApi;
import com.tanhua.dubbo.mapper.UserReportMapper;
import com.tanhua.dubbo.pojo.Option;
import com.tanhua.dubbo.pojo.UserReport;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService(version = "1.0.0")
public class UserReportApiImpl implements UserReportApi {

    @Autowired
    private UserReportMapper userReportMapper;

    @Override
    public UserReport queryByUserId(String userId) {
        QueryWrapper<UserReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.userReportMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer insert(UserReport userReport) {
        return this.userReportMapper.insert(userReport);
    }

    @Override
    public UserReport queryNew() {
        QueryWrapper<UserReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("update");
        return this.userReportMapper.selectOne(queryWrapper);
    }

    @Override
    public List<UserReport> querySimilarList(Long reportId,Long questionnaireId,Long userId) {
        QueryWrapper<UserReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("report_id", reportId).eq("questionnaire_id",questionnaireId).ne("user_id",userId);
        return this.userReportMapper.selectList(queryWrapper);
    }

    @Override
    public int updateReport(UserReport userReport, Long userId) {
        QueryWrapper<UserReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("questionnaire_id",userReport.getQuestionnaireId());
        return this.userReportMapper.update(userReport, queryWrapper);
    }

    @Override
    public UserReport queryByUserIdReportId(Long userId, Long reportId) {
        QueryWrapper<UserReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("report_id", reportId);
        return this.userReportMapper.selectOne(queryWrapper);
    }

    @Override
    public UserReport queryByUserIdLevel(Long userId, String level) {
        QueryWrapper<UserReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("level", level);
        UserReport userReport = this.userReportMapper.selectOne(queryWrapper);
        return userReport;
    }


}
